import resources.Constants;

import registnet.dao.CoursePeer;
import registnet.dao.Course;
import registnet.dao.Unit;
import registnet.dao.UnitPeer;

CoursePeer coursePeer = new CoursePeer(); 
Course course;

List data = new Vector();
List resultSet = new Vector();

boolean isShowingInfo;
boolean isSearching;


/* [start] 	initializes gui variables */
String _instance = bundle.getProperty("registnet.module.data_management.course");
String _instances = bundle.getProperty("registnet.module.data_management.courses");

String _course_code = bundle.getProperty("registnet.module.data_management.macro.courses.form.course_code");
String _course_title = bundle.getProperty("registnet.module.data_management.macro.courses.form.course_title");
String _course_description = bundle.getProperty("registnet.module.data_management.macro.courses.form.course_description");
String _units = bundle.getProperty("registnet.module.data_management.macro.courses.form.units");
String _semester_offered = bundle.getProperty("registnet.module.data_management.macro.courses.form.semester_offered");
String _prerequisite = bundle.getProperty("registnet.module.data_management.macro.courses.form.prerequisite");
String _unit_code = bundle.getProperty("registnet.module.data_management.macro.courses.form.unit_code");
String _lecture=  bundle.getProperty("registnet.module.data_management.macro.courses.form.lecture");
String _lecture_hours = bundle.getProperty("registnet.module.data_management.macro.courses.form.lecture_hours");
String _lecture_min_students = bundle.getProperty("registnet.module.data_management.macro.courses.form.lecture_min_students");
String _lecture_max_students = bundle.getProperty("registnet.module.data_management.macro.courses.form.lecture_max_students");
String _laboratory = bundle.getProperty("registnet.module.data_management.macro.courses.form.laboratory");
String _has_laboratory = bundle.getProperty("registnet.module.data_management.macro.courses.form.has_laboratory");
String _laboratory_hours = bundle.getProperty("registnet.module.data_management.macro.courses.form.laboratory_hours");
String _laboratory_min_students = bundle.getProperty("registnet.module.data_management.macro.courses.form.laboratory_min_students");
String _laboratory_max_students = bundle.getProperty("registnet.module.data_management.macro.courses.form.laboratory_max_students");
String _save = bundle.getProperty("registnet.module.data_management.macro.courses.form.save");
String _cancel = bundle.getProperty("registnet.module.data_management.macro.courses.form.cancel");
String _done = bundle.getProperty("registnet.module.data_management.macro.courses.form.done");

String _add = bundle.getProperty("registnet.module.data_management.actions.menu.add");
String _edit = bundle.getProperty("registnet.module.data_management.actions.menu.edit");
String _delete = bundle.getProperty("registnet.module.data_management.actions.menu.delete");
String _import = bundle.getProperty("registnet.module.data_management.actions.menu.import");
String _search = bundle.getProperty("registnet.module.data_management.actions.menu.search");
String _show_all = bundle.getProperty("registnet.module.data_management.actions.menu.show_all");

String _yes = Constants.YES;
String _no = Constants.NO;
String _registrar = Constants.REGISTRAR;
String _college = Constants.COLLEGE;
String _unit = Constants.UNIT;

String _proceed = bundle.getProperty("registnet.messages.proceed");
String _check = bundle.getProperty("registnet.messages.check");
String _successful = bundle.getProperty("registnet.messages.successful");
String _failed = bundle.getProperty("registnet.messages.failed");
String _select_one = bundle.getProperty("registnet.messages.select_one");
String _selected = bundle.getProperty("registnet.messages.selected");
String _use_search_result_viewer = bundle.getProperty("registnet.messages.use_search_result_viewer");

String _icon_add = bundle.getProperty("registnet.icons.add");
String _icon_edit = bundle.getProperty("registnet.icons.edit");
String _icon_delete = bundle.getProperty("registnet.icons.delete");
String _icon_import = bundle.getProperty("registnet.icons.import");
String _icon_search = bundle.getProperty("registnet.icons.search");
String _icon_show_all = bundle.getProperty("registnet.icons.show_all");
/* [end] */

/* setting the user for logs */
if(_user_type.equals(_registrar))
	coursePeer.setUser(_user, _registrar);
else
	coursePeer.setUser(_user, _office);
/* [end] */


/* [start]	prepares the tree */
ArrayList parentList = new ArrayList();
Map mapSTN_parent = new HashMap();
Map mapAL_parent = new HashMap();
Map mapAL_child = new HashMap();
Map map_treeitem = new HashMap();
SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
/* [end] */



/* [start]	defines the renderer for the tree */
public class MyTreeitemRenderer implements TreeitemRenderer {
	public void render(Treeitem item, Object data) throws Exception {	
		String str = data.toString();
		Treerow tr = null;
		Treecell tc = new Treecell(str);
		if(item.getTreerow()==null){
			tr = new Treerow();
			tr.setParent(item);
			map_treeitem.put(str, item);
		}else{
			tr = item.getTreerow(); 
			tr.getChildren().clear();
		}
		tc.setParent(tr);
	}
}
/* [end] */



/* [start]	retrieves the courses and places them on the tree;
			limits the courses to retrieve depending on the user type */
public void retrieveObjects(){
	map_treeitem.clear();
	mapSTN_parent.clear();
	mapAL_parent.clear();
	mapAL_child.clear();
	parentList.clear();
	
	List units;
	if (_user_type.equals(_registrar))
		units = new UnitPeer().getAllUnits();
	else if (_user_type.equals(_college))
		units = new UnitPeer().getUnitByCollege(new CollegePeer().getCollegeId(_office));
	else if (_user_type.equals(_unit)){
		units = new Vector();
		units.add(new UnitPeer().getUnit(_office));
	}
	for(int i=0; i<units.size(); i++){
		List courses = coursePeer.getCoursesByUnit(units.get(i).getUnitId());
		ArrayList alChild = new ArrayList();
		ArrayList alLeaf = new ArrayList();
		for(int j=0; j<courses.size(); j++){
			SimpleTreeNode stnChild = new SimpleTreeNode(courses.get(j).getCourseCode(), new ArrayList());
			alLeaf.add(courses.get(j).getCourseCode());
			alChild.add(stnChild);
		}
		SimpleTreeNode stnParent = new SimpleTreeNode(units.get(i).getUnitCode(), alChild);
		mapSTN_parent.put(units.get(i).getUnitCode(), stnParent);
		mapAL_parent.put(units.get(i).getUnitCode(), alChild);
		mapAL_child.put(units.get(i).getUnitCode(), alLeaf);
		parentList.add(stnParent);
	}
	SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
	SimpleTreeModel stm = new SimpleTreeModel(root);
	instance_tree.setModel(stm);
	instance_tree.setTreeitemRenderer(new MyTreeitemRenderer());
}
/* [end] */



/* [start]	shows all the entries */
void showAll(){
	retrieveObjects();
	try{
		instance_tree.setSelectedItem(map_treeitem.get(parentList.get(0).getData()));
	}catch(Exception e){}
	try{
		title.setValue("");
		contents.removeChild(contents.getFirstChild());
	}catch(Exception e ){}
}
/* [end] */



/* [start]	defines an event listener for the items on the tree;
			when the item is selected, its info is displayed */
private class SelectEventListener implements EventListener {
	public SelectEventListener (){}			
	public void onEvent(Event event) throws UiException {
		showInfo();
	}
}	
/* [end] */



/* [start]	defines an event listener for the cancel button;
			when fired, it deselects the items on the tree */
private class CancelEventListener implements EventListener {
	public SubmitEventListener (){}
	public void onEvent(Event event) throws UiException {
		instance_tree.clearSelection();
		try{
			title.setValue("");
			contents.removeChild(contents.getFirstChild());
		} catch (Exception e) {}
	}
}
/* [end] */



/* [start]	defines an event listener for the search button */
private class SearchEventListener implements EventListener {
	public SearchEventListener (){}
	public void onEvent(Event event) throws UiException {
		int isConfirmed = Messagebox.show(_use_search_result_viewer, "", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
		if (isConfirmed == Messagebox.YES){
			resultSet = coursePeer.searchCourse(course_code.value, course_title.value, course_description.value, nunits.value, new java.lang.Integer(1), semester_offered.value, grammar.Converter.infixToPrefix(prerequisite.value), lecture_hour_count.value, minimum_lecture_student_count.value, maximum_lecture_student_count.value, Constants.MAP_ANSWER_KEY.get(has_laboratory.value), laboratory_hour_count.value, minimum_laboratory_student_count.value, maximum_laboratory_student_count.value, new UnitPeer().getUnitId(unit_code.value));
			showSearchResultViewer();
		}
		else
			returnSearchResults();
			
		try{
			title.setValue("");
			contents.removeChild(contents.getFirstChild());
		} catch (Exception e) {}
	}
}
/* [end] */



/* [start]	replaces the contents of the tree with the result set of the search */
void returnSearchResults(){
	map_treeitem.clear();
	mapSTN_parent.clear();
	mapAL_parent.clear();
	mapAL_child.clear();
	parentList.clear();
	resultSet = coursePeer.searchCourse(course_code.value, course_title.value, course_description.value, nunits.value, new java.lang.Integer(1), semester_offered.value, grammar.Converter.infixToPrefix(prerequisite.value), lecture_hour_count.value, minimum_lecture_student_count.value, maximum_lecture_student_count.value, Constants.MAP_ANSWER_KEY.get(has_laboratory.value), laboratory_hour_count.value, minimum_laboratory_student_count.value, maximum_laboratory_student_count.value, new UnitPeer().getUnitId(unit_code.value));
	ArrayList alChild = new ArrayList();
	ArrayList alLeaf = new ArrayList();
	for(int i=0; i<resultSet.size(); i++){
		SimpleTreeNode stnChild = new SimpleTreeNode(resultSet.get(i).getCourseCode(), new ArrayList());
		alChild.add(stnChild);
		alLeaf.add(resultSet.get(i).getCourseCode());
	}
	SimpleTreeNode parentNode = new SimpleTreeNode("Search Result", alChild);
	parentList.add(parentNode);
	mapSTN_parent.put("Search Result", parentNode);
	mapAL_parent.put("Search Result", alChild);
	mapAL_child.put("Search Result", alLeaf);
	SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
	SimpleTreeModel stm = new SimpleTreeModel(root);
	instance_tree.setModel(stm);
	
	if (map_treeitem.get("Search Result").getTreechildren() != null)
		map_treeitem.get("Search Result").setOpen(true);
}
/* [end] */



/* [start] displays the info of the selected item */
void showInfo(){
	isShowingInfo = true;
	isSearching = false;
	if (contents.getFirstChild() != null){
		contents.removeChild(contents.getFirstChild());
	}
	try{
		if (instance_tree.getSelectedCount() == 1){
			if (instance_tree.getSelectedItem().getParentItem() != null){
				course = coursePeer.getCourse(instance_tree.getSelectedItem().getLabel());
				title.setValue(course.getCourseName());
				Component show_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/course_form.zul", contents, null);
				show_instance_form.getFellow("course_code_l").setValue(course.getCourseCode());
				show_instance_form.getFellow("course_title_l").setValue(course.getCourseName());
				show_instance_form.getFellow("course_description_l").setValue(course.getCourseDescription());
				show_instance_form.getFellow("nunits_l").setValue(course.getUnits().toString());
				show_instance_form.getFellow("semester_offered_l").setValue(Constants.MAP_SEMESTER_OFFERING_KEY.get(course.getSemesterOffered()));
				show_instance_form.getFellow("prerequisite_l").setValue(grammar.Converter.prefixToInfix(course.getPrerequisite()));
				show_instance_form.getFellow("unit_code_l").setValue(course.getUnit().getUnitName());
				show_instance_form.getFellow("lecture_hour_count_l").setValue(course.getLectureHoursPerWeek());
				show_instance_form.getFellow("minimum_lecture_student_count_l").setValue(course.getMinimumNumberOfLectureStudents());
				show_instance_form.getFellow("maximum_lecture_student_count_l").setValue(course.getMaximumNumberOfLectureStudents());
				show_instance_form.getFellow("has_laboratory_l").setValue(Constants.MAP_ANSWER_VALUE.get(course.getHasLaboratory()));
				if (course.getHasLaboratory() == Constants.MAP_ANSWER_KEY.get(Constants.YES)){
					show_instance_form.getFellow("laboratory_hour_count_r").visible = true;
					show_instance_form.getFellow("laboratory_hour_count_l").setValue(course.getLaboratoryHoursPerWeek());
					show_instance_form.getFellow("minimum_laboratory_student_count_r").visible = true;
					show_instance_form.getFellow("minimum_laboratory_student_count_l").setValue(course.getMinimumNumberOfLaboratoryStudents());
					show_instance_form.getFellow("maximum_laboratory_student_count_r").visible = true;
					show_instance_form.getFellow("maximum_laboratory_student_count_l").setValue(course.getMaximumNumberOfLaboratoryStudents());	
				}
				show_instance_form.getFellow("save_btn").detach();
				show_instance_form.getFellow("cancel_btn").detach();
			}
			else
				title.setValue("");
		}
		else if (instance_tree.getSelectedCount() > 1)
			title.setValue("" + instance_tree.getSelectedCount() + " " + _instance + " " + _selected);
		else
			title.setValue("");
	}catch (Exception e) { title.setValue(""); }
}
/* [end] */



/* [start]	displays the form for creating a new or editing an existing entry */
void showForm(String action){
	isShowingInfo = false;
	isSearching = false;
	try{
		contents.removeChild(contents.getFirstChild());
	} catch (Exception e) {}
	if (action.equals(_edit)){
		if(instance_tree.getSelectedCount() == 1){
			if(instance_tree.getSelectedCount() == 1){
				Component edit_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/course_form.zul", contents, null);
				title.setValue(_edit + " " + course.getCourseCode());
				edit_instance_form.getFellow("course_code").setValue(course.getCourseCode());
				edit_instance_form.getFellow("course_title").setValue(course.getCourseName());
				edit_instance_form.getFellow("course_description").setValue(course.getCourseDescription());
				edit_instance_form.getFellow("nunits").setValue(course.getUnits());
				edit_instance_form.getFellow("semester_offered").setValue(course.getSemesterOffered());
				edit_instance_form.getFellow("prerequisite").setValue(grammar.Converter.prefixToInfix(course.getPrerequisite()));
				edit_instance_form.getFellow("unit_code").setValue(course.getUnit().getUnitCode());
				if (_user_type.equals(_unit))
					edit_instance_form.getFellow("unit_code_r").setVisible(false);
				edit_instance_form.getFellow("lecture_hour_count").setValue(course.getLectureHoursPerWeek());
				edit_instance_form.getFellow("minimum_lecture_student_count").setValue(course.getMinimumNumberOfLectureStudents());
				edit_instance_form.getFellow("maximum_lecture_student_count").setValue(course.getMaximumNumberOfLectureStudents());
				edit_instance_form.getFellow("has_laboratory").setValue(Constants.MAP_ANSWER_VALUE.get(course.getHasLaboratory()));
				if (course.getHasLaboratory() == Constants.MAP_ANSWER_KEY.get(Constants.YES)){
					edit_instance_form.getFellow("laboratory_hour_count_r").visible = true;
					edit_instance_form.getFellow("laboratory_hour_count").setValue(course.getLaboratoryHoursPerWeek());
					edit_instance_form.getFellow("minimum_laboratory_student_count_r").visible = true;
					edit_instance_form.getFellow("minimum_laboratory_student_count").setValue(course.getMinimumNumberOfLaboratoryStudents());
					edit_instance_form.getFellow("maximum_laboratory_student_count_r").visible = true;
					edit_instance_form.getFellow("maximum_laboratory_student_count").setValue(course.getMaximumNumberOfLaboratoryStudents());	
				}
				edit_instance_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener(action));
				edit_instance_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
			}
			else
				title.setValue("");
		}
		else{
			alert(_selected_one + _instance);
		}
	}	
	else if (action.equals(_add)){
		Component add_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/course_form.zul", contents, null);
		title.setValue(_add + " " + _instance);
		instance_tree.clearSelection();
		if (_user_type.equals(_unit)){
			add_instance_form.getFellow("unit_code").setValue(_office);
			add_instance_form.getFellow("unit_code_r").setVisible(false);
		}
		add_instance_form.getFellow("save_btn").addEventListener("onClick", new SubmitEventListener(action));
		add_instance_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
	}

}
/* [end] */



/* [start]	import data from text file(s) */
void importData(){
	org.zkoss.util.media.Media media = Fileupload.get();
	if(media != null){
		java.io.Reader r = new java.io.BufferedReader(media.getReaderData());
		String line = null;
		StringBuffer buf = new StringBuffer(9999999);
		while ((line = r.readLine()) != null) {
			buf.append(line+"\r\n"); 
		}
		coursePeer.importCourses(buf.toString());
		retrieveObjects();
		title.setValue(_import + " " + _instance + " " + _successful);
	} 
}
/* [end] */



/* [start]	creates a new entry;
			updates the tree */
void createNewCourse(){
	if (isValid()){
		if (coursePeer.createNewCourse(course_code.value, course_title.value, course_description.value, nunits.value, new java.lang.Integer(1), semester_offered.value, grammar.Converter.infixToPrefix(prerequisite.value), lecture_hour_count.value, minimum_lecture_student_count.value, maximum_lecture_student_count.value, Constants.MAP_ANSWER_KEY.get(has_laboratory.value), laboratory_hour_count.value, minimum_laboratory_student_count.value, maximum_laboratory_student_count.value, new UnitPeer().getUnitId(unit_code.value)) != null){
			SimpleTreeNode newNode = new SimpleTreeNode(course_code.value, new ArrayList());
			String parentValue;			
			if(mapAL_parent.containsKey(unit_code.value))
				parentValue = unit_code.value;
			else
				parentValue = "Search Result";
			mapAL_parent.get(parentValue).add(newNode);
			mapAL_child.get(parentValue).add(course_code.value);
			if(mapAL_parent.get(parentValue).size() == 1){
				instance_tree.renderItem(map_treeitem.get(parentValue));
				map_treeitem.get(parentValue).setOpen(true);
			}
			else
				instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), mapAL_parent.get(parentValue).size() - 1, mapAL_parent.get(parentValue).size() - 1, org.zkoss.zul.event.TreeDataEvent.INTERVAL_ADDED);
			try{
				contents.removeChild(contents.getFirstChild());
			} catch (Exception e) { }
		}
		else
			alert(_check);
		
	}
	else{
		alert(_check);
	}
}
/* [end] */



/* [start]	edits an existing entry;
			updates the tree */
void editCourse(){		
	if (isValid()){
		try{
			Unit unit = coursePeer.getCourse(instance_tree.getSelectedItem().getLabel()).getUnit();
			if (coursePeer.updateCourse(coursePeer.getCourseId(instance_tree.getSelectedItem().getLabel()), course_code.value, course_title.value, course_description.value, nunits.value, new java.lang.Integer(1), semester_offered.value, grammar.Converter.infixToPrefix(prerequisite.value), lecture_hour_count.value, minimum_lecture_student_count.value, maximum_lecture_student_count.value, Constants.MAP_ANSWER_KEY.get(has_laboratory.value), laboratory_hour_count.value, minimum_laboratory_student_count.value, maximum_laboratory_student_count.value, new UnitPeer().getUnitId(unit_code.value))){
				String parentValue;
					if(mapAL_parent.containsKey(unit_code.value))
						parentValue = unit_code.value;
					else
						parentValue = "Search Result";
					if(unit.getUnitCode().equals(unit_code.value) || parentValue.equals("Search Result")){
						SimpleTreeNode newNode = new SimpleTreeNode(course_code.value, new ArrayList());
						mapAL_parent.get(parentValue).set(mapAL_child.get(parentValue).indexOf(instance_tree.getSelectedItem().getLabel()), newNode);
						mapAL_child.get(parentValue).set(mapAL_child.get(parentValue).indexOf(instance_tree.getSelectedItem().getLabel()), course_code.value);
						instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), mapAL_child.get(parentValue).indexOf(course_code.value), mapAL_child.get(parentValue).indexOf(course_code.value), org.zkoss.zul.event.TreeDataEvent.CONTENTS_CHANGED);
					}
					else{
						SimpleTreeNode newNode = new SimpleTreeNode(course_code.value, new ArrayList());
						mapAL_parent.get(parentValue).add(newNode);
						mapAL_child.get(parentValue).add(course_code.value);
						if(mapAL_parent.get(parentValue).size() == 1){
							map_treeitem.get(parentValue).setOpen(false);
							instance_tree.renderItem(map_treeitem.get(parentValue));
							map_treeitem.get(parentValue).setOpen(true);
						}
						else
							instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), mapAL_parent.get(parentValue).size() - 1, mapAL_parent.get(parentValue).size() - 1, org.zkoss.zul.event.TreeDataEvent.INTERVAL_ADDED);
						int index = mapAL_child.get(unit.getUnitCode()).indexOf(instance_tree.getSelectedItem().getLabel());
						mapAL_parent.get(unit.getUnitCode()).remove(index);
						mapAL_child.get(unit.getUnitCode()).remove(index);					
						instance_tree.getModel().fireEvent(mapSTN_parent.get(unit.getUnitCode()), index, index, org.zkoss.zul.event.TreeDataEvent.INTERVAL_REMOVED);						
						if (mapSTN_parent.get(unit.getUnitCode()).isLeaf())
							mapAL_parent.get(unit.getUnitCode()).add(new SimpleTreeNode("", new ArrayList()));						
					}				
					title.setValue(_edit + " " + _instance + " " + _successful);
					instance_tree.clearSelection();
					try{
						contents.removeChild(contents.getFirstChild());
					} catch (Exception e) {  }
			}
			else
				alert(_check);

			
		} catch (Exception e) { alert(_check); }
	}
	else
		alert(_check);
}
/* [end] */



/* [start]	deletes an entry */
void delete(){
	if(instance_tree.getSelectedCount() != 0){
		int isConfirmed = Messagebox.show("Proceed?", _delete + " " + _instance, Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
		if (isConfirmed == Messagebox.YES){
			boolean isError = false;
			int errorCount = 0;
			Iterator i = instance_tree.getSelectedItems().iterator();
			List unitCodes = new Vector();
			List courseCodes = new Vector();
			while(i.hasNext()){
				Treeitem treeitem = i.next(); 
				Course temp = coursePeer.getCourse(treeitem.getLabel());
				if (temp != null){
					if (coursePeer.deleteCourse(temp.getCourseId())){
						unitCodes.add(temp.getUnit().getUnitCode());
						courseCodes.add(temp.getCourseCode());
					}
					else{
						isError = true;
						errorCount++;
					}
				}
			}
			for(int i=0; i<unitCodes.size(); i++){
				String parentValue;
				if(mapSTN_parent.containsKey("Search Result"))
					parentValue = "Search Result";
				else
					parentValue = unitCodes.get(i);
				int index = mapAL_child.get(parentValue).indexOf(courseCodes.get(i));
				mapAL_parent.get(parentValue).remove(index);
				mapAL_child.get(parentValue).remove(index);
				instance_tree.getModel().fireEvent(mapSTN_parent.get(parentValue), index, index, org.zkoss.zul.event.TreeDataEvent.INTERVAL_REMOVED);
				
				if (mapSTN_parent.get(parentValue).isLeaf())
					mapAL_parent.get(parentValue).add(new SimpleTreeNode("", new ArrayList()));
			}
			if (isError){ 
				alert(_delete + " " + _failed + ": " + errorCount + " " + _instance);
				title.setValue(_delete + " " + _instance + " " + _failed);
			}			
			else{
				title.setValue(_delete + " " + _instance + " " + _successful);
				if (contents.getFirstChild() != null){
					contents.removeChild(contents.getFirstChild());
				}
			}
		}			
	}
	else
		alert(_select_one + _course);	
}
/* [end] */



/* [start] validates the form */
boolean isValid(){
	if (nunits.value == null)
		return false;
	if (semester_offered.value == null)
		return false;
	if (lecture_hour_count.value == null)
		return false;
	if (minimum_lecture_student_count.value == null)
		return false;
	if (maximum_lecture_student_count.value == null)
		return false;
	if (prerequisite.value.endsWith("AND ") || prerequisite.value.endsWith("OR "))
		return false;
	return true;
}
/* [end] */



/* [start]	defines an event listener for the submit button */
private class SubmitEventListener implements EventListener {
	String submitType;
	public SubmitEventListener (String submitType){
		this.submitType = submitType; 
	}
			
	public void onEvent(Event event) throws UiException {
		if(this.submitType.equals(_add))
			createNewCourse();
		else if (this.submitType.equals(_edit))
			editCourse();
	}
}
/* [end] */



/* [start]	filters the contents of the tree based on the
			what was entered by the user */
void filter(String string){
	ListIterator i = data.listIterator();
	List list = new java.util.Vector();
	while(i.hasNext()){
		String temp = i.next().getCourseCode(); 
		if(temp.toUpperCase().startsWith(string.toUpperCase())){
			list.add(temp);
		}
	}
	instance_tree.getModel().clear();
	instance_tree.getModel().addAll(list);
}
/* [end] */



/* [start]	filters the units to retrieve courses from */
List units;
if (_user_type.equals(_registrar))
	units = new UnitPeer().getAllUnits();
else if (_user_type.equals(_college))
	units = new UnitPeer().getUnitByCollege(new CollegePeer().getCollegeId(_office));
/* [end] */


/* [start]	filters the contents of the listbox based on the
			what was entered by the user */
void filterUnit(String string){

	ListIterator i = units.listIterator();
	List list = new java.util.Vector();
	while(i.hasNext()){
		String temp = i.next().getUnitCode(); 
		if(temp.toUpperCase().startsWith(string.toUpperCase())){
			list.add(temp);
		}
	}
	unit_code_lb.getModel().clear();
	unit_code_lb.getModel().addAll(list);
}
/* [end] */













void showSearchForm(){
	isSearching = true;
	isShowingInfo = false;

	try{
		contents.removeChild(contents.getFirstChild());
	} catch (Exception e) {}

	Component search_instance_form = Executions.createComponents("/WEB-INF/macros/data_management/course_form.zul", contents, null);
	title.setValue(_search + " " + _instance);
	instance_tree.clearSelection();
	search_instance_form.getFellow("search_btn").addEventListener("onClick", new SearchEventListener());
	search_instance_form.getFellow("cancel_btn").addEventListener("onClick", new CancelEventListener());
}

void showSearchResultViewer(){
	Tab tab = new Tab("Search Result");
	Tabs tabs = stage.getTabs();
	tab.setClosable(true);
	tab.setParent(tabs);
	stage.setSelectedTab(tab);
		
	Tabpanel tabpanel = new Tabpanel();
	tabpanel.setSclass("background_light_blue");
	Tabpanels tabpanels = stage.getTabpanels();
	tabpanel.setParent(tabpanels);
	stage.setSelectedPanel(tabpanel);
	
	HashMap map = new HashMap();
	map.put("resultSet", resultSet);
	map.put("instance", _instance);
	
	Component search_result_viewer = Executions.createComponents("/WEB-INF/macros/data_management/search_result_viewer.zul", tabpanel, map);
	title.setValue(_search + " " + _instance);
}

